class DefaultDict(dict):
    def __init__(self, default):
        self._d = default

    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            r = self[item] = self._d() if callable(self._d) else self._d
            return r



